#!/bin/sh

if [ $# -ne 1 ]
then
    echo "usage: $0 service-name"
    exit 201;
fi

SRC_HOME=/home/brochen/workspaces/juxing/$1
EXT_BASE=/workspace/juxingapps/ext
EXT_HOME=$EXT_BASE/$1-provider

if [ ! -d "$SRC_HOME" ]
then
    echo "folder $SRC_HOME is not exist."
    exit 1
fi

cd $SRC_HOME

#update source
#git pull origin
if [ $? != 0 ] 
then
    echo "fail to get source."
    exit 1
fi

#install
mvn clean package -Ptest
if [ $? != 0 ] 
then
    echo "fail to build source."
    exit 1
fi

#stop old process
if [ -d "$EXT_HOME" ]
then
    $EXT_HOME/bin/stop.sh
    if [ $? != 0 ] 
    then
        echo "fail to stop $1-provider."
        #exit 1
    fi  
fi

#copy tar.gz
cp $SRC_HOME/target/$1-provider-assembly.tar.gz $EXT_BASE/

#del old folder
cd $EXT_BASE
rm -rf $EXT_BASE/$1-provider

#unzip
tar -xzvf $1-provider-assembly.tar.gz

cd $1-provider/bin

#start service
./start.sh
if [ $? != 0 ] 
then
    echo "fail to start the service."
    exit 1
fi

